<template>
  <div>
    <div style="margin-bottom: 10px">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="员工ID">
          <el-input v-model="searchForm.employeeId" placeholder="请输入员工ID"></el-input>
        </el-form-item>
        <el-form-item label="工资月份">
          <el-date-picker
            v-model="searchForm.salaryMonth"
            type="month"
            placeholder="请选择工资月份"
            value-format="yyyy-MM"
            format="yyyy-MM"
            :clearable="true">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="部门">
          <el-select v-model="searchForm.departmentId" placeholder="请选择部门" clearable>
            <el-option
              v-for="item in departments"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职位">
          <el-select v-model="searchForm.positionId" placeholder="请选择职位" clearable>
            <el-option
              v-for="item in positions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetForm">重置</el-button>
          <el-button type="success" @click="exportToExcel">导出Excel</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="salaryInfoList" border stripe v-loading="loading">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="employeeId" label="员工ID" width="80"></el-table-column>
      <el-table-column label="员工信息" width="200">
        <template slot-scope="scope">
          <div v-if="scope.row.employee">
            <div>姓名：{{ scope.row.employee.name }}</div>
            <div>部门：{{ scope.row.employee.department ? scope.row.employee.department.name : '' }}</div>
            <div>职位：{{ scope.row.employee.position ? scope.row.employee.position.name : '' }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="salaryMonth" label="工资月份" width="100"></el-table-column>
      <el-table-column prop="basicSalary" label="基本工资" width="100"></el-table-column>
      <el-table-column prop="performanceSalary" label="绩效工资" width="100"></el-table-column>
      <el-table-column prop="overtimePay" label="加班费" width="100"></el-table-column>
      <el-table-column prop="others" label="其他补贴" width="100"></el-table-column>
      <el-table-column prop="utilityDeduction" label="水电费扣除" width="100"></el-table-column>
      <el-table-column prop="workingDays" label="工作天数" width="100"></el-table-column>
      <el-table-column prop="leaveHours" label="请假时数" width="100"></el-table-column>
      <el-table-column prop="payableSalary" label="应发工资" width="100"></el-table-column>
      <el-table-column prop="socialInsuranceDeduction" label="社保扣除" width="100"></el-table-column>
      <el-table-column prop="housingFundDeduction" label="住房公积金扣除" width="100"></el-table-column>
      <el-table-column prop="tax" label="个人所得税" width="100"></el-table-column>
      <el-table-column prop="actualSalary" label="实发工资" width="100"></el-table-column>
      <el-table-column prop="remarks" label="备注"></el-table-column>
      <el-table-column label="操作" width="150" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" @click="showDetail(scope.row)">详情</el-button>
          <el-button size="mini" type="primary" @click="printSalarySlip(scope.row)">打印</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 10px">
    </el-pagination>

    <!-- 工资单详情对话框 -->
    <el-dialog title="工资单详情" :visible.sync="detailDialogVisible" width="50%">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="员工ID">{{ currentDetail.employeeId }}</el-descriptions-item>
        <el-descriptions-item label="员工姓名">{{ currentDetail.employee ? currentDetail.employee.name : '' }}</el-descriptions-item>
        <el-descriptions-item label="部门">{{ currentDetail.employee && currentDetail.employee.department ? currentDetail.employee.department.name : '' }}</el-descriptions-item>
        <el-descriptions-item label="职位">{{ currentDetail.employee && currentDetail.employee.position ? currentDetail.employee.position.name : '' }}</el-descriptions-item>
        <el-descriptions-item label="工资月份">{{ currentDetail.salaryMonth }}</el-descriptions-item>
        <el-descriptions-item label="基本工资">{{ currentDetail.basicSalary }}</el-descriptions-item>
        <el-descriptions-item label="绩效工资">{{ currentDetail.performanceSalary }}</el-descriptions-item>
        <el-descriptions-item label="加班费">{{ currentDetail.overtimePay }}</el-descriptions-item>
        <el-descriptions-item label="其他补贴">{{ currentDetail.others }}</el-descriptions-item>
        <el-descriptions-item label="水电费扣除">{{ currentDetail.utilityDeduction }}</el-descriptions-item>
        <el-descriptions-item label="工作天数">{{ currentDetail.workingDays }}</el-descriptions-item>
        <el-descriptions-item label="请假时数">{{ currentDetail.leaveHours }}</el-descriptions-item>
        <el-descriptions-item label="应发工资">{{ currentDetail.payableSalary }}</el-descriptions-item>
        <el-descriptions-item label="社保扣除">{{ currentDetail.socialInsuranceDeduction }}</el-descriptions-item>
        <el-descriptions-item label="住房公积金扣除">{{ currentDetail.housingFundDeduction }}</el-descriptions-item>
        <el-descriptions-item label="个人所得税">{{ currentDetail.tax }}</el-descriptions-item>
        <el-descriptions-item label="实发工资">{{ currentDetail.actualSalary }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ currentDetail.remarks }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "SalSearch",
  data() {
    return {
      searchForm: {
        employeeId: '',
        salaryMonth: '',
        departmentId: '',
        positionId: ''
      },
      salaryInfoList: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      loading: false,
      departments: [],
      positions: [],
      detailDialogVisible: false,
      currentDetail: {}
    }
  },
  mounted() {
    this.initSalaryInfoList();
    this.loadDepartments();
    this.loadPositions();
  },
  methods: {
    initSalaryInfoList() {
      this.loading = true;
      let url = `/salary/info/page?page=${this.currentPage}&size=${this.pageSize}`;
      if (this.searchForm.employeeId) {
        url += `&employeeId=${this.searchForm.employeeId}`;
      }
      if (this.searchForm.salaryMonth) {
        url += `&salaryMonth=${this.searchForm.salaryMonth}`;
      }
      if (this.searchForm.departmentId) {
        url += `&departmentId=${this.searchForm.departmentId}`;
      }
      if (this.searchForm.positionId) {
        url += `&positionId=${this.searchForm.positionId}`;
      }
      
      this.getRequest(url).then(resp => {
        if (resp) {
          this.salaryInfoList = resp.records;
          this.total = resp.total;
        }
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },
    loadDepartments() {
      this.getRequest("/system/basic/department").then(resp => {
        if (resp) {
          this.departments = resp;
        }
      });
    },
    loadPositions() {
      this.getRequest("/system/basic/position").then(resp => {
        if (resp) {
          this.positions = resp;
        }
      });
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.initSalaryInfoList();
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      this.initSalaryInfoList();
    },
    handleSearch() {
      this.currentPage = 1;
      this.initSalaryInfoList();
    },
    resetForm() {
      this.searchForm = {
        employeeId: '',
        salaryMonth: '',
        departmentId: '',
        positionId: ''
      };
      this.currentPage = 1;
      this.initSalaryInfoList();
    },
    showDetail(row) {
      this.currentDetail = row;
      this.detailDialogVisible = true;
    },
    printSalarySlip(row) {
      // 实现打印功能
      this.$message.success('打印功能开发中...');
    },
    exportToExcel() {
      this.$confirm('是否导出所有工资单信息？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {
        this.getRequest("/salary/info/export", {}, 'blob').then(resp => {
          if (resp) {
            const blob = new Blob([resp], { type: 'application/vnd.ms-excel' });
            const link = document.createElement('a');
            link.href = window.URL.createObjectURL(blob);
            link.download = '工资单信息.xlsx';
            link.click();
            window.URL.revokeObjectURL(link.href);
            this.$message({
              type: 'success',
              message: '导出成功！'
            });
          }
        }).catch(error => {
          this.$message({
            type: 'error',
            message: '导出失败：' + (error.message || '未知错误')
          });
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消导出'
        });
      });
    }
  }
}
</script>

<style scoped>
.demo-form-inline {
  margin-bottom: 20px;
}
</style>